package com.woniuxy.exception;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.woniuxy.exception.customException.TheAddedHasBeenExist;
import com.woniuxy.resultCode.Result;
import com.woniuxy.resultCode.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //全局异常处理，没有指定异常类型，不管什么异常都可以捕获
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        log.error(e.getMessage());
        if(StringUtils.isNotBlank(e.getMessage())){
            return Result.error().code(5000).message(e.getMessage());
        }
        return Result.error()
                .code(ResultCode.UNKOWN_EXCEPTION
                        .getCode())
                .message(ResultCode.UNKOWN_EXCEPTION.getMessage());
    }

//    @ExceptionHandler(ArithmeticException.class)
//    @ResponseBody
//    public Result error(ArithmeticException e){
//        log.error(e.getMessage());
//        return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getCode())
//                .message(ResultCode.ARITHMETIC_EXCEPTION.getMessage());
//    }
//@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(ShiroException.class)
public Result handle2_401(ShiroException e) {
    return Result.error()
            .code(ResultCode.UNAUTHORIZATION
                    .getCode())
            .message(ResultCode.UNAUTHORIZATION.getMessage());
}

//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthenticatedException.class)
    public Result handle401(UnauthenticatedException e) {
        System.out.println(e.getMessage());
        return Result.error()
                .code(ResultCode.UNAUHENTICATION
                        .getCode())
                .message(ResultCode.UNAUHENTICATION.getMessage());
    }
    //令牌过期 "token已经过期，请重新登录"
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = TokenExpiredException.class)
    public Result handler_2(TokenExpiredException e) throws IOException {
        return Result.error().code(ResultCode.TRY_REFRESH.getCode()).message(ResultCode.TRY_REFRESH.getMessage());
    }
    //assert异常
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) throws IOException {
//        log.error("Assert异常:-------------->{}",e.getMessage());
        return Result.error().message(e.getMessage());
    }

    @ExceptionHandler(value = TheAddedHasBeenExist.class)
    public Result handler3(TheAddedHasBeenExist e) {
        return Result.error().message(e.getMessage());
    }
}
